﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.BackControllers.Home;
using System.Web.Mvc;
using Wwtfly.BBS.BackControllers.Attributes;
using Wwtfly.BBS.BackModels.ViewModels.Users;
using Wwtfly.BBS.BackBLL.Users;
using Wwtfly.BBS.Framework.Common;
using Wwtfly.BBS.BackModels.Models.Users;
using Wwtfly.BBS.Enums.Common;
using Wwtfly.Utility;
using Wwtfly.BBS.BackModels;
using Wwtfly.LoggerUtility;

namespace Wwtfly.BBS.BackControllers.Users
{
    /// <summary>
    /// /用户 信息 控制器
    /// </summary>
    [HasFolder("Back")]
    public class BackUsersController : BackBaseController
    {

        /// <summary>
        /// 分页大小 
        /// </summary>
        private static readonly int PageSize = 15;

        //用户列表 页面
        public ActionResult Index(string pageParams)
        {

            ViewData["Title"] = "用户列表页";
            PageParams pp = !string.IsNullOrEmpty(pageParams) ? new PageParams(pageParams) :
                new PageParams() { PageIndex = 1, PageSize = PageSize };
            UsersViewModel viewModel = new UsersViewModel()
            {
                PageParams = pp,
                UsersPagedList = UsersBLL.GetUsersInfoPageList(pp, new UsersSearchModel())
            };
            return View(viewModel);
        }

        // 添加
        [HasPower(PowerTypeEnum.Add)]
        [ValidateInput(false)]
        public ActionResult Add(string id)
        {
            ViewData["Title"] = "添加";
            return Edit(id);
        }

        // 添加
        [HttpPost, HasPower(PowerTypeEnum.Add)]
        [ValidateInput(false)]
        public ActionResult Add(UsersViewModel viewModel)
        {
            return EditSave(viewModel);

        }
        // 编辑
        [HasPower(PowerTypeEnum.Edit)]
        [ValidateInput(false)]
        public ActionResult Update(string id)
        {
            ViewData["Title"] = "修改";
            return Edit(id);
        }


        [HttpPost, HasPower(PowerTypeEnum.Edit)]
        [ValidateInput(false)]
        public ActionResult Update(UsersViewModel viewModel)
        {
            return EditSave(viewModel);
        }


        /// <summary>
        /// 返回 新增,编辑 界面
        /// </summary>
        /// <returns></returns>
        private ActionResult Edit(string id)
        {
            UsersViewModel viewModel = new UsersViewModel()
            {
                Users = UsersBLL.GetInfo(id.AsInt(0)),
            };
            return View("Edit", viewModel);
        }

        /// <summary>
        /// 编辑保存
        /// </summary>
        /// <returns></returns>
        private ActionResult EditSave(UsersViewModel viewModel)
        {

            //验证...
            List<string> errMessageList = this.ValidateData(viewModel);
            if (errMessageList != null && errMessageList.Count > 0)
            {
                ErrorViewModel errorViewModel = new ErrorViewModel()
                {
                    InfoList = errMessageList
                };
                return this.ErrorResult(errorViewModel);
            }

            try
            {
                if (viewModel.Users.Id <= 0)
                {
                    viewModel.Users.CreateTime = DateTime.Now;
                    viewModel.Users.CreateUser = User.UserInfo.Id;
                    viewModel.Users.LastModifyTime = DateTime.Now;
                    viewModel.Users.LastModifyUser = User.UserInfo.Id;

                    UsersBLL.Add(viewModel.Users);
                }
                else
                {
                    UsersModel model = UsersBLL.GetInfo(viewModel.Users.Id);

                    model.LastModifyTime = DateTime.Now;
                    model.LastModifyUser = User.UserInfo.Id;
                    UsersBLL.Update(model);
                }
                return this.Json(new
                {
                    result = "100",
                    html = string.Format("{0}", "保存成功!")
                });
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "BackNewsController.cs",
                    FuncCode = "EditSave(UsersViewModel viewModel)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "编辑保存.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }

            return this.Json(new
            {
                result = "-100",
                title = "警告信息",
                html = string.Format("请尝试刷新页面或联系管理员.")
            });
        }

        /// <summary>
        /// 验证 相关...
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        private List<string> ValidateData(UsersViewModel viewModel)
        {
            List<string> errMessageList = new List<string>();
            return errMessageList;
        }


    }
}
